//
//  SystemNotificationViewController.m
//  HAODE
//
//  Created by LiYinDi on 15/12/30.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "SystemNotificationViewController.h"
#import "RDVTabBarController.h"
#import "HttpRequest.h"
#import "EGORefreshTableHeaderView.h"
//#import "SystemNotificatioanTableViewCell.h"
#import "SystemNotificationCell.h"
#import "SystemNotificationModel.h"
#import "UITableView+FDTemplateLayoutCell.h"
@interface SystemNotificationViewController ()<EGORefreshTableHeaderDelegate>
{
    UIButton *allReadBtn;
    NSUInteger _PageIndex;
    NSUInteger _TotalCount;
    NSUInteger _TotalPage;
    
    NSMutableArray *modelArray;
    BOOL _reloading;
}
@end

@implementation SystemNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    modelArray=[NSMutableArray array];
    [_notificationListTableView registerClass:[SystemNotificationCell class] forCellReuseIdentifier:@"SystemNotificatioanIdentifier"];
    [self setNavigation];
    [self setAutoLayout];
    _PageIndex=0;
    [self loadMessageListNextPage:_PageIndex];
}
-(void) loadMessageListNextPage:(NSUInteger)oldPageNum {
    if(oldPageNum!=0 && _TotalCount<=modelArray.count)
        return;
    if(oldPageNum == 0){
        [modelArray removeAllObjects];
        [_notificationListTableView reloadData];
    }
    _PageIndex=oldPageNum+1;
    [self requestServiceList: _PageIndex pageSize:20];
}
-(void)requestServiceList:(NSUInteger)pageIndex pageSize:(NSUInteger)pageSize
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    if (Token ==nil) {
        Token=@"dd";
    }
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:Token forKey:@"tokenId"];
    NSNumber *pageSizeNumber = [NSNumber numberWithInteger:pageSize];
    NSNumber *pageIndexNumber = [NSNumber numberWithInteger:pageIndex];
    [params setObject:pageSizeNumber forKey:@"PageSize"];
    [params setObject:pageIndexNumber forKey:@"PageIndex"];
    
    [Global showIndeterminateHud:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    [HttpRequest requestData:@"UserCenter/Message/List" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        
       // NSLog(@"wwwww===%@",severDataDict.dataDict);
        if (isSuccess) {
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                _TotalCount=[[severDataDict.dataDict objectForKey:@"TotalCount"] integerValue];
                
                if (_TotalCount%20!=0) {
                    _TotalPage=_TotalCount/20+1;
                }
                else if (_TotalCount%20==0)
                {
                    _TotalPage=_TotalCount/20;
                }
                NSMutableArray *mArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                
                //     NSLog(@"mOrderArray===%@",mOrderArray);
                if ( mArray.count>0) {
                    for (NSDictionary *dic in mArray)  {
                        SystemNotificationModel *model=[SystemNotificationModel dataModelWithDict:dic];
                        [modelArray addObject:model];
                        
                        
                    }
                }
//                NSLog(@"mArray///===%@",mArray);
//                NSLog(@"modelArray///===%@",modelArray);
                [_notificationListTableView reloadData];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return modelArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return [tableView fd_heightForCellWithIdentifier:@"SystemNotificatioanIdentifier" cacheByIndexPath:indexPath configuration:^(SystemNotificationCell *cell) {
        cell.model = modelArray[indexPath.row];
    }];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//        static NSString * const  ServiceManagementIdentifier = @"SystemNotificatioanIdentifier";
//        SystemNotificationCell *cell=( SystemNotificationCell *)[tableView dequeueReusableCellWithIdentifier: ServiceManagementIdentifier];
//        if (cell==nil) {
//            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"SystemNotificatioanTableViewCell" owner:self options:nil];
//            cell = [array objectAtIndex:0];
//            
//        }
    SystemNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SystemNotificatioanIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model=modelArray[indexPath.row];
//    cell.messageContentLabel.text=cell.model.messageContent;
//    NSLog(@"mmmm=====%@",cell.messageContentLabel.text);
        if (indexPath.row == [modelArray count] - 1)
        {
            if (_PageIndex<=_TotalPage) {
                //  NSLog(@"###LOADING???");
                [self loadMessageListNextPage:_PageIndex];
            }
        }
        return cell;
   
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
-(void)setNavigation
{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    self.navigationController.navigationBarHidden=NO;
        self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    UILabel* titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,40, 23)];
    titleLabel.text=@"系统通知";
//    titleLabel.font=[UIFont systemFontOfSize: 19];
    self.navigationItem.titleView=titleLabel;
    
    allReadBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 1, 1, 1)];
    [allReadBtn setTitle:@"全部已读" forState:UIControlStateNormal];
    
    [allReadBtn setTitleColor:[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0]forState:UIControlStateNormal];
    allReadBtn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
    allReadBtn.width=80;
    allReadBtn.height=44;
    [allReadBtn addTarget:self action:@selector(allReadBtn) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightBtnItem = [[UIBarButtonItem alloc] initWithCustomView:allReadBtn];
    self.navigationItem.rightBarButtonItem = rightBtnItem;
    
}
-(void)allReadBtn
{
    
}
-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)setAutoLayout
{
    
    [_notificationListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        
        make.bottom.equalTo(self.view.mas_bottom) ;
        
        
        make.top.equalTo(self.view.mas_top) ;
    }];
    
}

@end
